using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MicroMsg.UI.UserContrl
{
	public class PanoramaSlideCtrl : UserControl
	{
		public delegate void SlideToNextSnsInfoEvent(object sender, object object_0);
		private enum SlideType
		{
			SlideToRight,
			SlideToLeft
		}
		private const string TAG = "PanoramaSlideCtrl";
		public AlbumSlideImage[] albumImage = new AlbumSlideImage[3];
		public int nowIndex;
		public int nowMIndex;
		private int pOldIndex = 1;
		private List<TimeLineObject> mTloList = new List<TimeLineObject>();
		private bool bIfInited;
		private PanoramaSlideCtrl.SlideType mSlideType;
		private bool bSlideWithDel;
		internal Grid LayoutRoot;
		internal Panorama panoramaCtrl;
		internal PanoramaItem pItem1;
		internal Grid Grid1;
		internal PanoramaItem pItem2;
		internal Grid Grid2;
		internal PanoramaItem pItem3;
		internal Grid Grid3;
		private bool _contentLoaded;
		public event PanoramaSlideCtrl.SlideToNextSnsInfoEvent SlideToNextSnsInfo;
		public List<TimeLineObject> tloList
		{
			get
			{
				return this.mTloList;
			}
			set
			{
				this.mTloList = value;
			}
		}
		public PanoramaSlideCtrl()
		{
			this.InitializeComponent();
			this.panoramaCtrl.set_Visibility(1);
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(1);
			base.add_Loaded(new RoutedEventHandler(this.PanoramaSlideCtrl_Loaded));
		}
		private void PanoramaSlideCtrl_Loaded(object sender, RoutedEventArgs e)
		{
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.panoramaCtrl.set_Visibility(0);
			});
		}
		public void InitData()
		{
			if (!this.bIfInited)
			{
				this.InitSlideImage();
				this.bIfInited = true;
			}
		}
		private void InitSlideImage()
		{
			if (this.tloList == null)
			{
				return;
			}
			Log.smethod_5("PanoramaSlideCtrl", string.Concat(new object[]
			{
				"nowindex = ",
				this.nowIndex,
				"  tloList.count = ",
				this.tloList.get_Count()
			}));
			this.albumImage[0] = new AlbumSlideImage(false);
			this.albumImage[1] = new AlbumSlideImage(false);
			this.albumImage[2] = new AlbumSlideImage(false);
			this.Grid1.get_Children().Add(this.albumImage[0]);
			this.Grid2.get_Children().Add(this.albumImage[1]);
			this.Grid3.get_Children().Add(this.albumImage[2]);
			this.albumImage[1].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex).content.mediaList.get_Item(this.nowMIndex));
			if (this.nowMIndex > 0)
			{
				this.albumImage[0].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex).content.mediaList.get_Item(this.nowMIndex - 1));
			}
			else
			{
				if (this.nowIndex > 0)
				{
					this.albumImage[0].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Item(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Count() - 1));
				}
			}
			if (this.nowMIndex + 1 < this.tloList.get_Item(this.nowIndex).content.mediaList.get_Count())
			{
				this.albumImage[2].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex).content.mediaList.get_Item(this.nowMIndex + 1));
				return;
			}
			if (this.nowIndex + 1 < this.tloList.get_Count())
			{
				this.albumImage[2].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex + 1).content.mediaList.get_Item(0));
			}
		}
		private void panoramaCtrl_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
		}
		private void panoramaCtrl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			if (this.nowIndex == 0 && this.nowMIndex == 0 && e.get_DeltaManipulation().get_Translation().get_X() > 0.0)
			{
				e.set_Handled(true);
				e.Complete();
				return;
			}
			if (this.nowIndex == this.tloList.get_Count() - 1 && this.nowMIndex == this.tloList.get_Item(this.tloList.get_Count() - 1).content.mediaList.get_Count() - 1 && e.get_DeltaManipulation().get_Translation().get_X() < 0.0)
			{
				e.set_Handled(true);
				e.Complete();
			}
		}
		private void panoramaCtrl_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
		}
		private void panoramaCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			int selectedIndex = this.panoramaCtrl.SelectedIndex;
			if (selectedIndex == this.pOldIndex)
			{
				return;
			}
			if ((selectedIndex - this.pOldIndex + 3) % 3 == 1)
			{
				this.mSlideType = PanoramaSlideCtrl.SlideType.SlideToRight;
			}
			else
			{
				this.mSlideType = PanoramaSlideCtrl.SlideType.SlideToLeft;
			}
			if ((this.mSlideType == PanoramaSlideCtrl.SlideType.SlideToLeft && this.nowIndex == 0 && this.nowMIndex == 0) || (this.mSlideType == PanoramaSlideCtrl.SlideType.SlideToRight && this.nowIndex == this.tloList.get_Count() - 1 && this.nowMIndex == this.tloList.get_Item(this.tloList.get_Count() - 1).content.mediaList.get_Count() - 1))
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(this.pOldIndex);
				});
				return;
			}
			this.pOldIndex = selectedIndex;
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.RefreshTloListNowIndex(this.mSlideType);
				this.LoadNearbyImg(this.mSlideType);
				this.RequestNearbyImg(this.nowIndex, this.nowMIndex);
			});
		}
		private void RefreshTloListNowIndex(PanoramaSlideCtrl.SlideType sType)
		{
			if (sType == PanoramaSlideCtrl.SlideType.SlideToLeft)
			{
				if (this.nowMIndex > 0)
				{
					this.nowMIndex--;
				}
				else
				{
					if (this.nowIndex <= 0)
					{
						return;
					}
					this.nowMIndex = this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Count() - 1;
					this.nowIndex--;
				}
			}
			else
			{
				if (this.bSlideWithDel)
				{
					if (this.SlideToNextSnsInfo != null)
					{
						SlideLocArgs slideLocArgs = new SlideLocArgs();
						slideLocArgs.nowIndex = this.nowIndex;
						slideLocArgs.nowMIndex = this.nowMIndex;
						this.SlideToNextSnsInfo(this, slideLocArgs);
					}
					return;
				}
				if (this.nowMIndex < this.tloList.get_Item(this.nowIndex).content.mediaList.get_Count() - 1)
				{
					this.nowMIndex++;
				}
				else
				{
					if (this.nowIndex + 1 >= this.tloList.get_Count())
					{
						return;
					}
					this.nowMIndex = 0;
					this.nowIndex++;
				}
			}
			if (this.SlideToNextSnsInfo != null)
			{
				SlideLocArgs slideLocArgs2 = new SlideLocArgs();
				slideLocArgs2.nowIndex = this.nowIndex;
				slideLocArgs2.nowMIndex = this.nowMIndex;
				this.SlideToNextSnsInfo(this, slideLocArgs2);
			}
		}
		private void LoadNearbyImg(PanoramaSlideCtrl.SlideType sType)
		{
			if (sType == PanoramaSlideCtrl.SlideType.SlideToLeft || this.bSlideWithDel)
			{
				if (this.nowMIndex > 0)
				{
					this.albumImage[(this.panoramaCtrl.SelectedIndex + 2) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex).content.mediaList.get_Item(this.nowMIndex - 1));
				}
				else
				{
					if (this.nowIndex > 0)
					{
						this.albumImage[(this.panoramaCtrl.SelectedIndex + 2) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Item(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Count() - 1));
					}
					else
					{
						this.albumImage[(this.panoramaCtrl.SelectedIndex + 2) % 3].ClearImage();
					}
				}
			}
			if (sType == PanoramaSlideCtrl.SlideType.SlideToRight || this.bSlideWithDel)
			{
				if (this.nowMIndex < this.tloList.get_Item(this.nowIndex).content.mediaList.get_Count() - 1)
				{
					this.albumImage[(this.panoramaCtrl.SelectedIndex + 1) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex).content.mediaList.get_Item(this.nowMIndex + 1));
				}
				else
				{
					if (this.nowIndex < this.tloList.get_Count() - 1)
					{
						this.albumImage[(this.panoramaCtrl.SelectedIndex + 1) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex + 1).content.mediaList.get_Item(0));
					}
					else
					{
						this.albumImage[(this.panoramaCtrl.SelectedIndex + 1) % 3].ClearImage();
					}
				}
			}
			this.bSlideWithDel = false;
		}
		private void RequestNearbyImg(int Index, int mIndex)
		{
			if (this.tloList.get_Item(Index).content.mediaList.get_Count() > mIndex + 2)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index).content.mediaList.get_Item(mIndex + 2));
			}
			if (Index + 1 >= this.tloList.get_Count())
			{
				return;
			}
			if (this.tloList.get_Item(Index).content.mediaList.get_Count() > mIndex + 1)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index + 1).content.mediaList.get_Item(0));
			}
			else
			{
				if (this.tloList.get_Item(Index + 1).content.mediaList.get_Count() > 1)
				{
					this.RequestImgFunc(this.tloList.get_Item(Index + 1).content.mediaList.get_Item(1));
				}
				else
				{
					if (Index + 2 < this.tloList.get_Count())
					{
						this.RequestImgFunc(this.tloList.get_Item(Index + 2).content.mediaList.get_Item(0));
					}
				}
			}
			if (mIndex >= 2)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index).content.mediaList.get_Item(mIndex - 2));
			}
			if (Index == 0)
			{
				return;
			}
			if (mIndex == 1)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index - 1).content.mediaList.get_Item(this.tloList.get_Item(Index - 1).content.mediaList.get_Count() - 1));
				return;
			}
			if (this.tloList.get_Item(Index - 1).content.mediaList.get_Count() > 1)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index - 1).content.mediaList.get_Item(this.tloList.get_Item(Index - 1).content.mediaList.get_Count() - 2));
				return;
			}
			if (Index - 2 >= 0)
			{
				this.RequestImgFunc(this.tloList.get_Item(Index - 2).content.mediaList.get_Item(this.tloList.get_Item(Index - 2).content.mediaList.get_Count() - 1));
			}
		}
		private void RequestImgFunc(TloMedia tloMedia)
		{
			snsMediaDownloadInfo mediaInfo = new snsMediaDownloadInfo
			{
				string_0 = tloMedia.strUrl,
				urlType = SnsUrlType.MMSNS_URL_CDN,
				priority = SnsPriority.HIGH
			};
			SnsMediaMgr.getImg(mediaInfo, true);
		}
		public void RemoveTloMediaByIndex(int index)
		{
			if (this.nowIndex < 0 || this.nowIndex >= this.tloList.get_Count())
			{
				Log.smethod_1("PanoramaSlideCtrl", "Index error: nowIndex = " + this.nowIndex);
				return;
			}
			if (this.nowIndex != 0)
			{
				this.albumImage[(this.panoramaCtrl.SelectedIndex + 2) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Item(this.tloList.get_Item(this.nowIndex - 1).content.mediaList.get_Count() - 1));
				this.nowMIndex = 0;
				this.bSlideWithDel = true;
				this.GoPrevious();
				return;
			}
			if (this.tloList.get_Count() == 1)
			{
				return;
			}
			this.albumImage[(this.panoramaCtrl.SelectedIndex + 1) % 3].LoadTloMediaImage(this.tloList.get_Item(this.nowIndex + 1).content.mediaList.get_Item(0));
			this.nowMIndex = 0;
			this.bSlideWithDel = true;
			this.GoNext();
		}
		public void GoPrevious()
		{
			if (this.nowIndex - 1 < 0 && this.nowMIndex - 1 < 0)
			{
				Log.smethod_1("PanoramaSlideCtrl", "Index error: nowIndex - 1 = " + (this.nowIndex - 1).ToString() + " , nowMIndex - 1 = " + (this.nowMIndex - 1).ToString());
				return;
			}
			int num = (this.panoramaCtrl.SelectedIndex + 2) % 3;
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(num);
		}
		public void GoNext()
		{
			if (this.nowIndex + 1 >= this.tloList.get_Count() && this.nowMIndex + 1 >= this.tloList.get_Item(this.nowIndex).content.mediaList.get_Count() - 1)
			{
				Log.smethod_1("PanoramaSlideCtrl", "Index error: nowIndex + 1 = " + (this.nowIndex + 1).ToString() + " , tloList.Count = " + this.tloList.get_Count().ToString());
				Log.smethod_1("PanoramaSlideCtrl", "Index error: nowMIndex + 1 = " + (this.nowMIndex + 1).ToString() + " , tloList[nowIndex].content.mediaList.Count - 1 = " + (this.tloList.get_Item(this.nowIndex).content.mediaList.get_Count() - 1).ToString());
				return;
			}
			int num = (this.panoramaCtrl.SelectedIndex + 1) % 3;
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(num);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Album/UI/PanoramaSlideCtrl.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panoramaCtrl = (Panorama)base.FindName("panoramaCtrl");
			this.pItem1 = (PanoramaItem)base.FindName("pItem1");
			this.Grid1 = (Grid)base.FindName("Grid1");
			this.pItem2 = (PanoramaItem)base.FindName("pItem2");
			this.Grid2 = (Grid)base.FindName("Grid2");
			this.pItem3 = (PanoramaItem)base.FindName("pItem3");
			this.Grid3 = (Grid)base.FindName("Grid3");
		}
	}
}
